#include "Animation.h"


Animation::Animation(SDL_Surface *image , int nbSprit , double time){
	this->images = image;
	this->tailleSprit = image->w/nbSprit;
	this->time = time;
	this->nbSprit = nbSprit;
	nbAnim = 0;
	tempsDernier = SDL_GetTicks()/100.00;

}

void Animation::lancer(){
	tempsDernier = SDL_GetTicks()/100.00;
	nbAnim = 0;
}

int Animation::getNbSpritAfficher(){
	return nbAnim;
}

void Animation::passerSprit(){
	nbAnim++;
}


SDL_Surface *Animation::getImage(){
    return images;
}

SDL_Surface *Animation::getSpritAct(){
	SDL_Surface *animAct = SDL_CreateRGBSurface(SDL_HWSURFACE , tailleSprit , images->h , 32, 0, 0, 0, 0) ;
	SDL_SetColorKey(animAct, SDL_SRCCOLORKEY, SDL_MapRGB(animAct->format, 0, 0, 0));

	SDL_Rect posNouv;
	posNouv.x = 0;
	posNouv.y = 0;
	//on fait un modulo pour toujour revenir au debut
	SDL_Rect posImage;
	posImage.x = tailleSprit*(nbAnim%nbSprit) ;
	posImage.y = 0;
	posImage.w = tailleSprit;
	posImage.h = images->h;
	SDL_BlitSurface(images,&posImage,animAct,&posNouv);
	//SDL_SetColorKey(animAct, SDL_SRCCOLORKEY, SDL_MapRGB(animAct->format, 255, 255, 255));

	//si on a passer assez de temps alors on passe d'image
	double timeAct = SDL_GetTicks()/100.00;

	if(timeAct - tempsDernier > time){
		nbAnim++;
		tempsDernier = timeAct;
	}

	return animAct;
}



